﻿using System;
using Microsoft.BizTalk.ExplorerOM;
using System.Collections.Generic;
using System.Configuration;

namespace BizTalkOneService{

    public class BizTalkOrchestrations
    {
        private int Started = 0;
        private int Enlisted = 0;
        private int Unenlisted = 0;
        private int Total = 0;

        public BizTalkOrchestrations()
        {
            BtsCatalogExplorer catalog = new BtsCatalogExplorer();
            catalog.ConnectionString = Convert.ToString(ConfigurationManager.ConnectionStrings["BizTalkConnectionString"]);
            foreach (BtsAssembly assembly in catalog.Assemblies)
            {
                foreach (BtsOrchestration orch in assembly.Orchestrations)
                {
                    if (orch.Status == OrchestrationStatus.Enlisted) { Enlisted++; }
                    if (orch.Status == OrchestrationStatus.Started) { Started++; }
                    if (orch.Status == OrchestrationStatus.Unenlisted) { Unenlisted++; }
                    Total++;
                }
            }
        }

        public List<Counter> GetOrchestrations() 
        {
            List<Counter> CounterList = new List<Counter>();
            string strStarted = String.Format("Started ({0})", this.Total);
            string strStopped = String.Format("Stopped ({0})", this.Total);
            string strUnenlisted = String.Format("Unenlisted ({0})", this.Total);
            string strTotal = String.Format("Total ({0})", this.Total);
            CounterList.Add(new Counter() { CounterCategory = "Orchestration", CounterName = strStarted, CounterValue = Started });
            CounterList.Add(new Counter() { CounterCategory = "Orchestration", CounterName = strStopped, CounterValue = Enlisted });
            CounterList.Add(new Counter() { CounterCategory = "Orchestration", CounterName = strUnenlisted, CounterValue = Unenlisted });
            CounterList.Add(new Counter() { CounterCategory = "Orchestration", CounterName = strTotal, CounterValue = Total });
            return CounterList;
        }
    }
}